﻿using System;
using System.Windows.Forms;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.VanBanThamDuyet
{
    public partial class frmVanBanThamDuyetList : frmList
    {
        public frmVanBanThamDuyetList()
        {
            InitializeComponent();
        }

        protected override void Add()
        {
            using (var frm = new frmVanBanThamDuyetDetail())
            {
                frm.OnLoadData += LoadData;
                frm.ShowDialog();
            }
        }

        protected override void LoadData()
        {
            var biz = new VanBanThamDuyetBiz();

            grvVanBanThamDuyet.DataSource = null;
            grvVanBanThamDuyet.Refresh();
            grvVanBanThamDuyet.DataSource = biz.GetAll();
            txtTen.Text = string.Empty;
        }

        #region Edit

        protected override void Edit()
        {
            EditData();
        }

        private void EditData()
        {
            if (grvVanBanThamDuyet.SelectedRows.Count > 0)
            {
                var selectedRow = grvVanBanThamDuyet.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmVanBanThamDuyetDetail(id))
                {
                    fEdit.OnLoadData += LoadData;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        #endregion
        

        private void DeleteData()
        {
            try
            {
                if (grvVanBanThamDuyet.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvVanBanThamDuyet.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new Model.VanBanThamDuyetPCCC
                        {
                            Id = id
                        };

                        var biz = new VanBanThamDuyetBiz();
                        biz.Delete(model);
                        LoadData();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected override void Delete()
        {
            DeleteData();
        }

        private void grvVanBanThamDuyet_SelectionChanged(object sender, EventArgs e)
        {
            var gv = (DataGridView)sender;
            foreach (DataGridViewRow row in gv.SelectedRows)
            {
                txtTen.Text = row.Cells[1].Value.ToString();
            }
        }
    }
}
